package at.sii.n4w;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ReadFragment extends Fragment {

	private static final String ARG_NFCDATA = "nfcdata";
	public static final String TAG = "NFC4WLAN";
	protected static final String ARG_SSID = "ssid";
	protected static final String ARG_PWD = "pwd";
	public static Bundle args;
	View rootView;


	public ReadFragment() {

	}

	public static Fragment newInstance() {
		ReadFragment fragment = new ReadFragment();
		args = new Bundle();

		args.putString(ARG_NFCDATA, "empty");
		fragment.setArguments(args);
		return fragment;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.d(TAG, "Taginhalt: " + getArguments().getString(ARG_NFCDATA));
		rootView = inflater.inflate(R.layout.fragment_read, container, false);
		setText();
		Button btnWrite = (Button) rootView.findViewById(R.id.connect_button);
		btnWrite.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				WifiManager wifi = (WifiManager) getActivity()
						.getSystemService(Context.WIFI_SERVICE);
				WifiConfiguration wc = new WifiConfiguration();
				wc.SSID = "\"" + getArguments().getString(ARG_SSID) + "\"";
				wc.preSharedKey = "\"" + getArguments().getString(ARG_PWD)
						+ "\"";
				wc.hiddenSSID = true;
				wc.status = WifiConfiguration.Status.ENABLED;
				wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
				wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
				wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
				wc.allowedPairwiseCiphers
						.set(WifiConfiguration.PairwiseCipher.TKIP);
				wc.allowedPairwiseCiphers
						.set(WifiConfiguration.PairwiseCipher.CCMP);
				wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
				int res = wifi.addNetwork(wc);
				Log.d("WifiPreference", "add Network returned " + res);
				boolean b = wifi.enableNetwork(res, true);
				Log.d("WifiPreference", "enableNetwork returned " + b);
				wifi.saveConfiguration();
				Toast.makeText(getActivity(),
						"Connected to " + getArguments().getString(ARG_SSID),
						Toast.LENGTH_SHORT).show();
			}
		});
		return rootView;
	}

	public void onResume() {
		super.onResume();
		setText();
		Log.d(TAG, "read onResume");
	}

	public void onPause() {
		super.onPause();
		Log.d(TAG, "read onPause");
	}

	public void refreshData(String result) {
		args.putString(ARG_NFCDATA, result);
		setText();
		Log.d(TAG, "read refreshData " + getArguments().getString(ARG_NFCDATA));
	}

	public void setText() {
		TextView textView = (TextView) rootView.findViewById(R.id.tag_content);
		if (getArguments().getString(ARG_NFCDATA).contains("@#@")) {
			args.putString(ARG_SSID, getArguments().getString(ARG_NFCDATA)
					.split("@#@")[0]);
			args.putString(ARG_PWD, getArguments().getString(ARG_NFCDATA)
					.split("@#@")[1]);

			textView.setText("SSID: " + getArguments().getString(ARG_SSID)
					+ " PWD: " + getArguments().getString(ARG_PWD));
		} else {
			textView.setText("not supported format: "
					+ getArguments().getString(ARG_NFCDATA));
		}
	}
}
